package com.huangjue.backend.controller;

import com.huangjue.backend.entity.Message;
import com.huangjue.backend.service.MessageService;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/messages")
@CrossOrigin(origins = "*")
//通知
public class MessageController {
    @Autowired
    private MessageService messageService;

    //找该人所有通知
    @GetMapping("/user/{userId}")
    @Operation(summary = "getMessagesByUserId")
    public ResponseEntity<?> getMessagesByUserId(@PathVariable Integer userId) {
        Map<String,Object> response=new HashMap<>();
        List<Message> list = messageService.getMessagesByUserId(userId);
        response.put("success",true);
        response.put("message","获取成功");
        response.put("data",list);
        //测试数据返回是否有问题
//        System.out.println(response);
        return ResponseEntity.ok(response);
    }
    //新未读标志为已读
    @PostMapping("/read/{messageId}")
    @Operation(summary = "markMessageRead")
    public ResponseEntity<?> markMessageRead(@PathVariable Integer messageId) {
        Map<String,Object> response=new HashMap<>();
        int result = messageService.markMessageRead(messageId);
        if (result >= 1){
            response.put("success",true);
            response.put("message","修改成功");
//          response.put("data",list);
            return ResponseEntity.ok(response);
        }else{
            response.put("success",false);
            response.put("message","修改失败");
//          response.put("data",list);
            return ResponseEntity.ok(response);
        }
    }

    //查找未读消息数量
    @GetMapping("/noRead/{userId}")
    @Operation(summary = "getNoReadMessageNumberByUserId")
    public ResponseEntity<?> getNoReadMessageNumberByUserId(@PathVariable Integer userId) {
        Map<String,Object> response=new HashMap<>();
        int count = messageService.selectNoReadMessageCount(userId);
        response.put("success",true);
        response.put("message","获取成功");
        response.put("data",count);
        return ResponseEntity.ok(response);
    }

} 